//
//  LoginAndRegisterController.m
//  Jlcf_ios
//
//  Created by Ji on 2018/9/26.
//  Copyright © 2018 ski. All rights reserved.
//

#import "rmMqyfJLCFLoginAndRegisterControllerFbzYtj.h"
#import "sHkPtEJLCFMineRequestozAYZs.h"
#import "DvPsQTJLCFRegisterViewControllerJsJjCP.h"
#import "JLcYhdJLCFLoginViewControllerPAPHbZ.h"
#import "dgXPYNJLCFInputViewjVJFSJ.h"

@interface rmMqyfJLCFLoginAndRegisterControllerFbzYtj ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *dZrvUEtextFieldRRHEAt;

@property (nonatomic, strong) UIButton *FDTTddnextBtnLAOJQZ;
@property (nonatomic, strong) dgXPYNJLCFInputViewjVJFSJ *CDozpXinputViewIsapkb;

@end

@implementation rmMqyfJLCFLoginAndRegisterControllerFbzYtj

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self addNavigationBar];
//    self.navigationBar.title = @"登录/注册";
    
    
//    [self configView];
    [self setUpLoginAndRegisterUI];
}

- (void)setUpLoginAndRegisterUI {
    self.KWcnnUtitleTipsLabelQLOdiQ.text = @"欢迎使用金鲤财富";
    self.NdgzxmdesLabelTTSqri.text = @"请输入您的手机号进行登陆或注册";
    [self.zNugwOsubmitBtnFCoXrL setTitle:@"下一步" forState:UIControlStateNormal];
    
    dgXPYNJLCFInputViewjVJFSJ *CDozpXinputViewIsapkb = [[dgXPYNJLCFInputViewjVJFSJ alloc] initWithInputType:InputTypeMessage];
    CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG.delegate = self;
    CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG.keyboardType = UIKeyboardTypeNumberPad;
    CDozpXinputViewIsapkb.ajptsrleftTipsImageViewgYkbmv.image = [UIImage imageNamed:@"icon_login_phone"];
    [self.contentView addSubview:CDozpXinputViewIsapkb];
    [CDozpXinputViewIsapkb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(FitRealValue(78));
        make.right.mas_equalTo(self.contentView.mas_right).offset(-FitRealValue(78));
        make.height.mas_equalTo(FitRealValue(80));
        make.top.mas_equalTo(self.NdgzxmdesLabelTTSqri.mas_bottom).offset(FitRealValue(80));
    }];
    self.zNugwOsubmitBtnFCoXrL.enabled = NO;
    self.zNugwOsubmitBtnFCoXrL.backgroundColor = QTZ_Gray153;
    [CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG addTarget:self action:@selector(textChanged) forControlEvents:UIControlEventEditingChanged];
    _CDozpXinputViewIsapkb = CDozpXinputViewIsapkb;
    [self.zNugwOsubmitBtnFCoXrL addTarget:self action:@selector(handleLogin) forControlEvents:UIControlEventTouchUpInside];
    weakify(self);
    CDozpXinputViewIsapkb.clicked = ^{
        strongify(self);
        [self textChanged];
    };
    [self.CDozpXinputViewIsapkb setInputTextPlaceHolder:@"请输入您的手机号"];
}

- (void)textChanged {
    if (self.CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG.text.length == 11) {
        self.zNugwOsubmitBtnFCoXrL.backgroundColor = Theme_Color;
        
        self.zNugwOsubmitBtnFCoXrL.enabled = YES;
    } else {
        self.zNugwOsubmitBtnFCoXrL.backgroundColor = QTZ_Gray153;
        self.zNugwOsubmitBtnFCoXrL.enabled = NO;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)configView
{
    
    UILabel *titleLabel = [UILabel labelWithText:@"欢迎使用金鲤财富" textColor:QTZ_Gray51 font:FONT(25)];
    titleLabel.left = 40;
    titleLabel.top = NAVBAR_HEIGHT + 75;
    [self.view addSubview:titleLabel];
    
    UILabel *edHyphtipsLabelTVYPVW = [UILabel labelWithText:@"请输入您的手机号进行登陆或注册" textColor:QTZ_Gray153 font:FONT(15)];
    edHyphtipsLabelTVYPVW.left = 40;
    edHyphtipsLabelTVYPVW.top = titleLabel.bottom + 16;
    [self.view addSubview:edHyphtipsLabelTVYPVW];
    
    self.dZrvUEtextFieldRRHEAt = [[UITextField alloc] initWithFrame:CGRectMake(38, edHyphtipsLabelTVYPVW.bottom + 60, FULL_WIDTH - 76, 25)];
    self.dZrvUEtextFieldRRHEAt.font = FONT(15);
    self.dZrvUEtextFieldRRHEAt.placeholder = @"请输入手机号码";
    self.dZrvUEtextFieldRRHEAt.keyboardType = UIKeyboardTypeNumberPad;
    self.dZrvUEtextFieldRRHEAt.returnKeyType = UIReturnKeyDone;
    self.dZrvUEtextFieldRRHEAt.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.dZrvUEtextFieldRRHEAt.delegate = self;
    [self.dZrvUEtextFieldRRHEAt addTarget:self action:@selector(strChanged) forControlEvents:UIControlEventEditingChanged];
    [self.dZrvUEtextFieldRRHEAt.layer addSublayer:[CALayer addLineOriginPoint:CGPointMake(0.0f, self.dZrvUEtextFieldRRHEAt.frame.size.height) toPoint:CGPointMake(self.dZrvUEtextFieldRRHEAt.frame.size.width, self.dZrvUEtextFieldRRHEAt.frame.size.height) color:QTZ_Gray132 borderWidth:.5]];
    [self.view addSubview:self.dZrvUEtextFieldRRHEAt];
    
    self.FDTTddnextBtnLAOJQZ = [[UIButton alloc] initWithFrame:CGRectMake(38, self.dZrvUEtextFieldRRHEAt.bottom + 35, self.dZrvUEtextFieldRRHEAt.width, 45)];
    self.FDTTddnextBtnLAOJQZ.titleLabel.font = FONT(12);
    [self.FDTTddnextBtnLAOJQZ setTitleColor:QTZ_White forState:0];
    [self.FDTTddnextBtnLAOJQZ setTitle:@"下一步" forState:0];
    [self.FDTTddnextBtnLAOJQZ setBackgroundColor:QTZ_Gray182];
    self.FDTTddnextBtnLAOJQZ.layer.cornerRadius = 5;
    [self.FDTTddnextBtnLAOJQZ addTarget:self action:@selector(handleLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.FDTTddnextBtnLAOJQZ];
    
    UIButton *serviceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:serviceButton];
    NSString *acceptText = @"遇到问题？联系客服: ";
    NSMutableAttributedString *acceptAttrStr = [[NSMutableAttributedString alloc] initWithString:acceptText attributes:@{NSForegroundColorAttributeName:QTZ_Gray153, NSFontAttributeName:FONT(12)}];
    NSString *serviceText = @"4000768558";
    NSMutableAttributedString *serviceAttrStr = [[NSMutableAttributedString alloc] initWithString:serviceText attributes:@{NSForegroundColorAttributeName:[UIColor blueColor], NSFontAttributeName:FONT(12)}];
    [acceptAttrStr appendAttributedString:serviceAttrStr];
    [serviceButton setAttributedTitle:acceptAttrStr forState:UIControlStateNormal];
    [serviceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-30);
        make.height.mas_equalTo(15);
    }];
    weakify(self);
    [serviceButton bk_addEventHandler:^(id sender) {
        dispatch_async(dispatch_get_main_queue(), ^{
            strongify(self);
            NSString *callStr = [NSString stringWithFormat:@"tel:%@", serviceText];;
            UIWebView *callWebview = [[UIWebView alloc] init];
            [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:callStr]]];
            [self.view addSubview:callWebview];
        });
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)strChanged {
    if (self.dZrvUEtextFieldRRHEAt.text.length == 11) {
        [self.FDTTddnextBtnLAOJQZ setBackgroundImage:[UIImage imageNamed:@"icon_mine_btnBg"] forState:0];
        
        self.FDTTddnextBtnLAOJQZ.enabled = YES;
    } else {
        [self.FDTTddnextBtnLAOJQZ setBackgroundImage:nil forState:0];
        self.FDTTddnextBtnLAOJQZ.enabled = NO;
    }
}

- (void)handleLogin {
    [sHkPtEJLCFMineRequestozAYZs verPhoneWithParams:@{@"phone" : self.CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG.text} loadingInView:self.view success:^(NSString *status) {
        
        if ([status isEqualToString:@"10002"]) { // 未注册
            [self showNotice:@"该手机号暂未注册"];
            DvPsQTJLCFRegisterViewControllerJsJjCP *vc = [DvPsQTJLCFRegisterViewControllerJsJjCP new];
            vc.UabOWQphoneNumberaYOEQN = self.CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG.text;
            [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
        } else if ([status isEqualToString:@"10001"]) { // 已注册
            JLcYhdJLCFLoginViewControllerPAPHbZ *vc =[JLcYhdJLCFLoginViewControllerPAPHbZ new];
            vc.UabOWQphoneNumberaYOEQN = self.CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG.text;
            [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
        }
        
    } failure:^(NSString *error) {
        [self showNotice:error];
    }];

}

- (BOOL)dZrvUEtextFieldRRHEAt:(UITextField *)dZrvUEtextFieldRRHEAt shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (dZrvUEtextFieldRRHEAt == self.CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.CDozpXinputViewIsapkb.jYmrASinputFieldXQCIYG.text.length >= 11) {
            self.dZrvUEtextFieldRRHEAt.text = [dZrvUEtextFieldRRHEAt.text substringToIndex:11];
            return NO;
        }
    }
    return YES;
}

@end
